<html>
<head><meta charset="utf-8"><title>procedural macro API panic · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html">procedural macro API panic</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="172106301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172106301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172106301">(Jul 31 2019 at 07:55)</a>:</h4>
<p>Is there a documentation for the panic "Procedural macro API is used outside of a procedural macro"?</p>



<a name="172106464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172106464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172106464">(Jul 31 2019 at 07:58)</a>:</h4>
<p>I'm building a procedural macro crate, which links and executes a native library, and the compiler crashes with that error (stacktrace: <a href="https://users.rust-lang.org/t/procedural-macro-api-is-used-outside-of-a-procedural-macro/30841" target="_blank" title="https://users.rust-lang.org/t/procedural-macro-api-is-used-outside-of-a-procedural-macro/30841">https://users.rust-lang.org/t/procedural-macro-api-is-used-outside-of-a-procedural-macro/30841</a>).</p>



<a name="172111585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172111585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172111585">(Jul 31 2019 at 09:21)</a>:</h4>
<p>I don't understand why the panic originates in the library: the library does not use the <code>proc_macro</code> API, but only the <code>proc_macro2</code> crate</p>



<a name="172117659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172117659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172117659">(Jul 31 2019 at 11:09)</a>:</h4>
<p><span class="user-mention" data-user-id="116427">@Federico</span> the proc_macro2 crate internally uses the proc_macro API</p>



<a name="172119879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172119879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172119879">(Jul 31 2019 at 11:50)</a>:</h4>
<p>@simulacrum I see, thanks! I expected <code>proc_macro2</code>to be a opaque wrapper around <code>proc_macro</code>, without the limitations of the latter.</p>



<a name="172120587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172120587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172120587">(Jul 31 2019 at 12:03)</a>:</h4>
<p>The limitations are sort of inherent and can't be removed by a library I believe</p>



<a name="172120607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172120607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172120607">(Jul 31 2019 at 12:03)</a>:</h4>
<p>(beyond duplicating the entire compiler, but even then you don't get what you want since the result won't work with rustc)</p>



<a name="172124282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124282">(Jul 31 2019 at 12:57)</a>:</h4>
<p>But what does "outside of a procedural macro" exactly mean? Outside the crate that implements a procedural macro?</p>



<a name="172124309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124309">(Jul 31 2019 at 12:57)</a>:</h4>
<p>the context is loosely I believe same-thread</p>



<a name="172124583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124583">(Jul 31 2019 at 13:00)</a>:</h4>
<p>just based on the snippets in that post I would expect it to work but more info might be needed on how exactly PLUGIN is defined/what it does</p>



<a name="172124870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124870">(Jul 31 2019 at 13:03)</a>:</h4>
<p>Can it be that also calling a native library crosses the boundary? The plugin is a Rust crate, linked as native library in order to allow the procedural macro crate to choose via an environment variable which implementation to use</p>



<a name="172124909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124909">(Jul 31 2019 at 13:03)</a>:</h4>
<p>The code is here: <a href="https://github.com/viperproject/rust-contracts" target="_blank" title="https://github.com/viperproject/rust-contracts">https://github.com/viperproject/rust-contracts</a></p>



<a name="172124967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124967">(Jul 31 2019 at 13:04)</a>:</h4>
<p>Plausible, yeah, I'm not entirely sure myself when TLS changes</p>



<a name="172124974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172124974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172124974">(Jul 31 2019 at 13:04)</a>:</h4>
<p>and/or is no longer accessible</p>



<a name="172125105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172125105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172125105">(Jul 31 2019 at 13:05)</a>:</h4>
<p>I would check if you can reproduce when not dynamically loading the library (just regular cargo dep)</p>



<a name="172125110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172125110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172125110">(Jul 31 2019 at 13:05)</a>:</h4>
<p>that way you can better isolate the failure</p>



<a name="172125152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172125152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172125152">(Jul 31 2019 at 13:05)</a>:</h4>
<p>you could also plausibly test if it's working by declaring via <code>thread_local!</code> a thread local and seeing if you can access the same local from a separate library and/or googling that</p>



<a name="172125221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172125221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172125221">(Jul 31 2019 at 13:06)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/9152fe4ea053a29469691349f4b63aa94c9aac56/src/libproc_macro/bridge/client.rs#L268-L271" target="_blank" title="https://github.com/rust-lang/rust/blob/9152fe4ea053a29469691349f4b63aa94c9aac56/src/libproc_macro/bridge/client.rs#L268-L271">https://github.com/rust-lang/rust/blob/9152fe4ea053a29469691349f4b63aa94c9aac56/src/libproc_macro/bridge/client.rs#L268-L271</a> is the relevant local afaik</p>



<a name="172125441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/procedural%20macro%20API%20panic/near/172125441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Poli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/procedural.20macro.20API.20panic.html#172125441">(Jul 31 2019 at 13:09)</a>:</h4>
<p>Thanks for the info!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>